﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Light_Calendar
{
    public enum RepeatType { None, Daily, Weekly, Monthly, Yearly }

    [Serializable]
    public class CalendarEvent
    {
        public bool IsNew { get; set; }
        [XmlAttribute]
        public int Id { get; set; }
        public string Description { get; set; }
        public DateTime Date { get; set; }
        public System.Windows.Media.Color Color { get; set; }
        public RepeatType Repeats { get; set; }
        public DateTime Until { get; set; }
        public int Reminder { get; set; }

        public CalendarEvent(DateTime date)
        {
            IsNew = true;
            Id = 0;
            Description = "New Event";
            Date = date;
            Color = Properties.Settings.Default.color;
            Repeats = RepeatType.None;
            Until = DateTime.MinValue;
            Reminder = Properties.Settings.Default.reminder;
        }

        public CalendarEvent() { } // Only for xml serialization
        
        public CalendarEvent(CalendarEvent e)
        {
            IsNew = e.IsNew;
            Id = e.Id;
            Description = e.Description;
            Date = e.Date;
            Color = e.Color;
            Repeats = e.Repeats;
            Until = e.Until;
            Reminder = e.Reminder;
        }

        public bool OccursOn(DateTime date)
        {
            if (date >= Date && (Until == DateTime.MinValue || date <= Until)) switch (Repeats)
                {
                    case RepeatType.None: return (date == Date);
                    case RepeatType.Daily: return true;
                    case RepeatType.Weekly: return (date.DayOfWeek == Date.DayOfWeek);
                    case RepeatType.Monthly: return (date.Day == Date.Day);
                    case RepeatType.Yearly: return (date.Month == Date.Month && date.Day == Date.Day);
                    default: return false;
                }
            else return false;
        }
    }
}
